package org.jeecg.modules.aliyun.entity.run;

import cn.hutool.core.util.RandomUtil;
import org.jeecg.modules.phone.entity.PhoneEntityTask;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;

import static org.jeecg.modules.aliyun.entity.run.TaskUtil.cmdBy4G;
import static org.jeecg.modules.aliyun.entity.run.TaskUtil.doCmdRun;

public class PhoneEntityTaskCommandUtil {



    public static String homeCommand(PhoneEntityTask phoneEntityTask) {
        return runAndCapterImg(phoneEntityTask, "input keyevent 3");
    }


    public static String backCommand(PhoneEntityTask phoneEntityTask) {
        return runAndCapterImg(phoneEntityTask, "input keyevent 4");
    }

    public static String clickCommand(PhoneEntityTask phoneEntityTask) {
//        269 478,
        return runAndCapterImg(phoneEntityTask, "input tap " + phoneEntityTask.getTaskStatus());
    }



    @NotNull
    private static String runAndCapterImg(PhoneEntityTask phoneEntityTask, String cmdStr) {
        boolean cmdRunFail =  doCmdRun(phoneEntityTask.getEntityId(), cmdStr);
        final String entityId = phoneEntityTask.getEntityId();
        if(cmdRunFail){
            new Thread( ()-> screenCap(entityId)).start();
            return "执行成功,正在传输截图";

        }
        return "执行失败";
    }

    private static void screenCap(String entityId) {
        String ScreenCapFile = "screen_" + entityId + RandomUtil.randomString(8) + ".png";
        doCmdRun( entityId,"screencap -p /sdcard/" + ScreenCapFile);
        doCmdRun( entityId,"smallSdcardPng" + ScreenCapFile);
        doCmdRun( entityId,"uploadFileToServer" + ScreenCapFile);
    }
 
}